package org.hzero.exam.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.exam.app.service.HodrSoLineService;
import org.hzero.exam.config.SwaggerTags;
import org.hzero.exam.domain.entity.HodrSoHeader;
import org.hzero.exam.domain.entity.HodrSoLine;
import org.hzero.exam.domain.repository.HodrSoHeaderRepository;
import org.hzero.exam.domain.repository.HodrSoLineRepository;
import org.hzero.mybatis.common.Criteria;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author 马旭阳
 * @date2019/8/6
 */
@Api(tags = SwaggerTags.LINE)
@RestController("lineController.v1")
@RequestMapping("/v1/{organizationId}/lines")
public class HodrSoLineController extends BaseController {
    @Autowired
    private HodrSoLineRepository soLineRepository;
    @Autowired
    private HodrSoHeaderRepository soHeaderRepository;
    @Autowired HodrSoLineService soLineService;
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "根据订单编号分页查询订单行信息")
    @GetMapping
    public ResponseEntity<Page> selectByOrderNumber(@PathVariable Long organizationId,
                                                    @RequestParam String orderNumber,
                                                    PageRequest pageRequest){
        HodrSoHeader hodrSoHeader = soHeaderRepository.selectByOrdernumber(orderNumber);
        return Results.success(PageHelper.doPage(pageRequest,()->soLineRepository.selectBySoheaderID(hodrSoHeader.getSoHeaderId())));
        }


    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "添加订单行信息")
    @PostMapping
    public ResponseEntity insertOne(@PathVariable Long organizationId,
                                    @RequestBody HodrSoLine soLine){
        if (soLine.getSoHeaderId()!=null) {
            soLineRepository.insert(soLine);
            return Results.success();
        }
        return Results.error();
    }
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "删除订单行信息")
    @DeleteMapping
    public ResponseEntity deleteOne(@PathVariable Long organizationId,
                                    @RequestBody HodrSoLine soLine){
        this.validObject(soLine);
        SecurityTokenHelper.validToken(soLine);
       soLineService.delete(soLine);
       return  Results.success();

    }
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "修改订单行信息")
    @PutMapping
    public ResponseEntity updateOne(@PathVariable Long organizationId,
                                    @RequestBody HodrSoLine soLine){
        this.validObject(soLine);
        SecurityTokenHelper.validToken(soLine);
        soLineService.update(soLine);
        return  Results.success();
    }
}
